package com.stock.stockana.requests;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.stock.stockana.beans.ResponseBean;
import com.stock.stockana.constants.NetWorkConstants;
import com.stock.stockana.events.network.DeleteStockEvent;

import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;

/**
 * Created by ljgsonx on 2016/1/23.
 */
public class DeleteStock extends BaseStringRequest<DeleteStock> {

    private final static String TAG = "DeleteStock--->";
    private static String stockNum;

    private DeleteStock() {
        super(Method.POST, NetWorkConstants.DELETE_STOCK_URL, listener, errorListener);
        Log.d(TAG, NetWorkConstants.DELETE_STOCK_URL);
    }

    static Response.Listener<String> listener = new Response.Listener<String>(){

        @Override
        public void onResponse(String response) {
            try{
                ResponseBean rb = JSON.parseObject(response, ResponseBean.class);
                DeleteStockEvent de = new DeleteStockEvent(rb);
                de.setDealedStockNum(stockNum);
                EventBus.getDefault().post(de);
            }catch (Exception e) {
                ResponseBean rb = new ResponseBean();
                rb.setRespCode(NetWorkConstants.RESPONSE_CODE_ERROR);
                rb.setErrorMsg(NetWorkConstants.RESPONSE_ERROR2 + response);
                EventBus.getDefault().post(new DeleteStockEvent(rb));
            }
        }
    };

    static Response.ErrorListener errorListener = new Response.ErrorListener(){

        @Override
        public void onErrorResponse(VolleyError error) {
            ResponseBean rb = new ResponseBean();
            rb.setRespCode(NetWorkConstants.RESPONSE_CODE_ERROR);
            rb.setErrorMsg(NetWorkConstants.RESPONSE_ERROR3 + error.toString());
            EventBus.getDefault().post(new DeleteStockEvent(rb));
        }
    };

    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        Map<String, String> map = new HashMap<>();
        map.put("stockNum", stockNum);
        return map;
    }

    public static DeleteStock build() {
        return new DeleteStock();
    }

    public DeleteStock setStockNum(String stockNum) {
        this.stockNum = stockNum;
        return this;
    }


}
